export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        try {
            const { sheet, row: addRow, rowCount: addRowCount = 1, table } = options;
            sheet.suspendPaint();
            const { row: dataRow } = table.dataRange();
            if (isUndo) {
                const { name, row, col, rowCount, colCount, columns, cellTypes, bindingPath, showHeader } = options.old;
                sheet.tables.remove(name);
                const oldTable = sheet.tables.add(name, row, col, rowCount, colCount, undefined, { showHeader });
                oldTable.filterButtonVisible(false);
                oldTable.bindColumns(columns.map(column => new GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField)));
                oldTable.bindingPath(bindingPath);
                cellTypes.forEach((cellType, index) => {
                    sheet.getCell(dataRow, col + index).cellType(cellType);
                });
            } else {
                const { row, col, name, rowCount, colCount, columns, bindingPath,showHeader } = table.toJSON();
                options.old = {
                    name, row, col, rowCount, colCount, columns, bindingPath,showHeader,
                    cellTypes: columns.map((d, index) => {
                        return sheet.getCell(dataRow, col + index).cellType();
                    })
                };
                sheet.tables.remove(name);
                const oldTable = sheet.tables.add(name, row, col, rowCount - 1, colCount, undefined, { showHeader });
                oldTable.filterButtonVisible(false);
                oldTable.bindColumns(columns.map(column => new GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField)));
                oldTable.bindingPath(bindingPath);
            }
            sheet.resumePaint();
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
    }
};